#Model behind the list views
#Data comes from the GUI Controller and
#Qt calls data() internally to update the list.

from PyQt4 import QtCore as QC

invindex = QC.QModelIndex()

class DynamicListModel(QC.QAbstractListModel):
    def __init__(self, in_prefixes, in_qparent=None):
        """Takes a list of string prefixes for the data to be tracked,
        stores it, initializes a list of zeroes with an entry
        for each prefix, and marks the list as started.
        """
        QC.QAbstractListModel.__init__(self, in_qparent)
        self.prefixes = in_prefixes
        self.entries = [0 for l in in_prefixes]
        
        self.started = True
    
    def rowCount(self, in_qparent):
        return len(self.entries)
    
    def data(self, in_qindex, in_qrole):
        ret = None
        if in_qrole == QC.Qt.DisplayRole:
            row = in_qindex.row()
            ret =  ("") if (self.prefixes[row] == "") else (self.prefixes[row]+" : "+str(self.entries[row]))
        return ret
    
    def refresh(self, in_entries):
        """Takes a list of values which you want put after each prefix,
        stores it, and signals a data change to Qt.
        """
        self.entries = in_entries
        l_qindex = self.index(0, 1, invindex)
        r_qindex = self.index(0, len(self.prefixes), invindex)
        self.emit(QC.SIGNAL("dataChanged(const QModelIndex &, const QModelIndex &)"), l_qindex, r_qindex)
                
    def reset_list(self, in_prefixes):
        """Takes a new list of prefixes for tracked data,
        stores it, and marks the list updated.
        """
        self.beginResetModel()
        self.prefixes = in_prefixes
        self.entries = [0 for l in in_prefixes]
        self.endResetModel()
        
        self.started = True